package net.mudfox.base;

import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;

import net.mudfox.helper.CommonHelper;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import static net.mudfox.helper.CommonHelper.JSON_TYPE;

public abstract class BaseRestActivity extends AppCompatActivity {
    protected String entry;
    protected String baseUrl;
    protected OkHttpClient client;
    protected ActionBar actionBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        actionBar = getSupportActionBar();
        if (actionBar != null)
            // 给左上角图标的左边加上一个返回的图标,对应ActionBar.DISPLAY_HOME_AS_UP
            actionBar.setDisplayHomeAsUpEnabled(true);
        client = new OkHttpClient();
    }

    public void httpGet(String url) {
        Request request = new Request.Builder()
                .url(url)
                .get() //默认就是GET请求，可以不写
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                CommonHelper.debug("HTTP GET ERROR!");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String reply = response.body().string();
                runOnUiThread(() -> onReceivedReply(reply));
            }
        });
    }

    public void httpPost(String url, String json) {
        RequestBody body = RequestBody.create(JSON_TYPE, json);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                CommonHelper.debug("HTTP PUT ERROR!");
            }

            @Override
            public void onResponse(Call call, Response response) {
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    public void onReceivedReply(String reply) {
        try {
            JSONObject object = JSON.parseObject(reply);
            if (!object.isEmpty())
                processReply(object);
        } catch (JSONException e) {
            CommonHelper.debug("ERROR: 错误的JSON格式！");
        }
    }

    public abstract void processReply(JSONObject rootObject);
}
